package com.szq.lottery.interfaces.facade;

import com.alibaba.fastjson.JSON;
import com.szq.lottery.application.process.IActivityProcess;
import com.szq.lottery.application.process.req.DrawProcessReq;
import com.szq.lottery.application.process.res.DrawProcessResult;
import com.szq.lottery.application.process.res.RuleQuantificationCrowdResult;
import com.szq.lottery.common.Constants;
import com.szq.lottery.domain.rule.model.req.DecisionMatterReq;
import com.szq.lottery.domain.strategy.model.vo.DrawAwardInfo;
import com.szq.lottery.interfaces.assembler.IMapping;
import com.szq.lottery.rpc.ILotteryActivityBooth;
import com.szq.lottery.rpc.dto.AwardDTO;
import com.szq.lottery.rpc.req.DrawReq;
import com.szq.lottery.rpc.req.QuantificationDrawReq;
import com.szq.lottery.rpc.res.DrawRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @author 吾空白
 * @description: 抽奖活动展台
 */
@Controller
public class LotteryActivityBooth implements ILotteryActivityBooth {

    private Logger logger= LoggerFactory.getLogger(LotteryActivityBooth.class);

    @Resource
    private IActivityProcess activityProcess;

    @Resource
    private IMapping<DrawAwardInfo, AwardDTO> awardMapping;

    @Override
    public DrawRes doDraw(DrawReq drawReq) {
        try {
            logger.info("抽奖，开始 uId:{} activity:{}",drawReq.getuId(),drawReq.getActivityId());
            //1.执行抽奖
            DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(new DrawProcessReq(drawReq.getuId(), drawReq.getActivityId()));
            if (!Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())){
                logger.error("抽奖，失败(抽奖过程异常) uId:{} activity:{}",drawReq.getuId(),drawReq.getActivityId());
                return new DrawRes(drawProcessResult.getCode(),drawProcessResult.getInfo());
            }
            //2.数据转换
            DrawAwardInfo drawAwardInfo = drawProcessResult.getDrawAwardInfo();
            AwardDTO awardDTO = awardMapping.sourceToTarget(drawAwardInfo);
            awardDTO.setActivityId(drawReq.getActivityId());
            //3.封装数据
            DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
            drawRes.setAwardDTO(awardDTO);
            logger.info("抽奖，完成 uId:{} activityId:{} drawRes:{}",drawReq.getuId(),drawReq.getActivityId(),drawRes);
            return  drawRes;
        }catch (Exception e){
            logger.error("抽奖，失败 uid:{},activity:{},reqJson:{}",drawReq.getuId(),drawReq.getActivityId(), JSON.toJSONString(drawReq),e);
            return new DrawRes(Constants.ResponseCode.UN_ERROR.getCode(),Constants.ResponseCode.UN_ERROR.getInfo());
        }
    }

    @Override
    public DrawRes doQuantificationDraw(QuantificationDrawReq quantificationDrawReq) {
       try {
           logger.info("量化人群抽奖，开始 uId:{} treeId:{}",quantificationDrawReq.getuId(),quantificationDrawReq.getTreeId());
           //1.执行规则引擎，获取用户可以参与的活动号
           RuleQuantificationCrowdResult ruleQuantificationCrowdResult = activityProcess.doRuleQuantificationCrowd(new DecisionMatterReq(quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId(), quantificationDrawReq.getValMap()));
           if (!Constants.ResponseCode.SUCCESS.getCode().equals(ruleQuantificationCrowdResult.getCode())){
               logger.error("量化人群抽奖，失败（规则引擎执行异常）uId:{} treeId:{}",quantificationDrawReq.getuId(),quantificationDrawReq.getTreeId());
               return new DrawRes(ruleQuantificationCrowdResult.getCode(),ruleQuantificationCrowdResult.getInfo());
           }
           //2.执行抽奖
           Long activityId = ruleQuantificationCrowdResult.getActivityId();
           DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(new DrawProcessReq(quantificationDrawReq.getuId(), activityId));
           if (!Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())){
               logger.error("量化人群抽奖，失败（抽奖过程异常）uId:{} treeId:{}",quantificationDrawReq.getuId(),quantificationDrawReq.getTreeId());
               return new DrawRes(drawProcessResult.getCode(),drawProcessResult.getInfo());
           }
           //3.数据转换
           DrawAwardInfo drawAwardInfo = drawProcessResult.getDrawAwardInfo();
           AwardDTO awardDTO = awardMapping.sourceToTarget(drawAwardInfo);
           awardDTO.setActivityId(activityId);
           //4.封装数据
           DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
           drawRes.setAwardDTO(awardDTO);
           logger.info("量化人群抽奖，完成 uId：{} treeId：{} drawRes：{}", quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId(), JSON.toJSONString(drawRes));

           return drawRes;
       }catch (Exception e) {
           logger.error("量化人群抽奖，失败 uId：{} treeId：{} reqJson：{}", quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId(), JSON.toJSONString(quantificationDrawReq), e);
           return new DrawRes(Constants.ResponseCode.UN_ERROR.getCode(), Constants.ResponseCode.UN_ERROR.getInfo());
       }
    }
}
